package com.a3dweijing.wetouch2.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.Util.ImageLoaderUtil;
import com.a3dweijing.wetouch2.bean.ImageList;
import com.a3dweijing.wetouch2.callback.OnClick;

import java.util.Collections;
import java.util.List;

/**
 * Created by yxd on 2017/11/3.
 */

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.MyViewHolder> {

    private List<ImageList> imageLists;
    private OnClick onClick;

    public ImageAdapter(List<ImageList> imageLists, OnClick onClick) {
        Collections.sort(imageLists);
        this.imageLists = imageLists;
        this.onClick=onClick;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image,parent,false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.name.setText(imageLists.get(position).getThree_d_img_name());
        ImageLoaderUtil.getLoader().loadImg4(imageLists.get(position).getThree_d_img_url(),holder.pic);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClick!=null){
                    onClick.onclick(position,imageLists.get(position));
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return imageLists.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView name;
        private ImageView pic;
        public MyViewHolder(View itemView) {
            super(itemView);
            name= (TextView) itemView.findViewById(R.id.image_name);
            pic= (ImageView) itemView.findViewById(R.id.image_pic);

        }
    }
}
